package cn.iocoder.yudao.module.train.dal.mysql.order;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.train.controller.admin.student.vo.StudentPageReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.OrdersDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.TeacherUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.course.CourseDO;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecommodity.CourseCommodityDO;
import cn.iocoder.yudao.module.train.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.train.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.train.dal.dataobject.viplevel.VipLevelDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.train.controller.admin.order.vo.*;

/**
 * 订单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    default List<OrderDO> selectList(OrderPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getTradeSn, reqVO.getTradeSn())
                .eqIfPresent(OrderDO::getOrderSn, reqVO.getOrderSn())
                .eqIfPresent(OrderDO::getStudentId, reqVO.getStudentId())
                .eqIfPresent(OrderDO::getCommodityId, reqVO.getCommodityId())
                .eqIfPresent(OrderDO::getLevelId, reqVO.getLevelId())
                .eqIfPresent(OrderDO::getOriginPrice, reqVO.getOriginPrice())
                .eqIfPresent(OrderDO::getSalePrice, reqVO.getSalePrice())
                .eqIfPresent(OrderDO::getDiscountAmount, reqVO.getDiscountAmount())
                .eqIfPresent(OrderDO::getRefundAmount, reqVO.getRefundAmount())
                .eqIfPresent(OrderDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OrderDO::getOrderType, reqVO.getOrderType())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }

    default PageResult<OrdersDTO> selectPage(OrderPageReqVO reqVO) {
        // 创建MyBatis-Plus分页对象
        Page<OrdersDTO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());

        // 创建连接查询条件
        MPJLambdaWrapper<OrderDO> wrapper = new MPJLambdaWrapper<OrderDO>()
                .selectAll(OrderDO.class)
                .select(StudentDO::getStudentName)
                .select(StudentDO::getStudentPhoneNumber)
                .leftJoin(StudentDO.class, on -> on
                        .eq(StudentDO::getId, OrderDO::getStudentId)
                )
                .select(VipLevelDO::getLevelName)
                .leftJoin(VipLevelDO.class,on -> on
                        .eq(VipLevelDO::getId,OrderDO::getLevelId))
                .selectAs(CourseDO::getName,OrdersDTO::getCommodityName)
                .leftJoin(CourseCommodityDO.class,CourseCommodityDO::getId,OrderDO::getCommodityId)
                .leftJoin(CourseDO.class,CourseDO::getId,CourseCommodityDO::getCourseId);  // 连接条件

        // 添加与原方法相同的过滤条件
        if (reqVO.getWxUserId() != null) {
            wrapper.eq(OrderDO::getWxUserId, reqVO.getWxUserId());
        }
        if (reqVO.getTradeSn() != null) {
            wrapper.eq(OrderDO::getTradeSn, reqVO.getTradeSn());
        }
        if (reqVO.getStudentPhoneNumber() != null) {
            wrapper.like(StudentDO::getStudentPhoneNumber, reqVO.getStudentPhoneNumber());
        }
        if (reqVO.getOrderSn() != null) {
            wrapper.eq(OrderDO::getOrderSn, reqVO.getOrderSn());
        }
        if (reqVO.getStudentName() != null) {
            wrapper.like(StudentDO::getStudentName, reqVO.getStudentName());
        }
        if (reqVO.getCommodityId() != null) {
            wrapper.eq(OrderDO::getCommodityId, reqVO.getCommodityId());
        }
        if (reqVO.getCommodityName() != null) {
            wrapper.like(CourseDO::getName, reqVO.getCommodityName());
        }
        if (reqVO.getLevelId() != null) {
            wrapper.eq(OrderDO::getLevelId, reqVO.getLevelId());
        }
        if (reqVO.getOriginPrice() != null) {
            wrapper.eq(OrderDO::getOriginPrice, reqVO.getOriginPrice());
        }
        if (reqVO.getSalePrice() != null) {
            wrapper.eq(OrderDO::getSalePrice, reqVO.getSalePrice());
        }
        if (reqVO.getDiscountAmount() != null) {
            wrapper.eq(OrderDO::getDiscountAmount, reqVO.getDiscountAmount());
        }
        if (reqVO.getRefundAmount() != null) {
            wrapper.eq(OrderDO::getRefundAmount, reqVO.getRefundAmount());
        }
        if (reqVO.getStatus() != null) {
            wrapper.eq(OrderDO::getStatus, reqVO.getStatus());
        }
        if (reqVO.getOrderType() != null) {
            wrapper.eq(OrderDO::getOrderType, reqVO.getOrderType());
        }
        if (reqVO.getCreateTime() != null && reqVO.getCreateTime().length == 2) {
            wrapper.between(OrderDO::getCreateTime, reqVO.getCreateTime()[0], reqVO.getCreateTime()[1]);
        }
        wrapper.orderByDesc(OrderDO::getId);

        // 执行连接查询并分页
        IPage<OrdersDTO> resultPage = selectJoinPage(page, OrdersDTO.class, wrapper);

        // 转换为框架的PageResult
        return new PageResult<>(resultPage.getRecords(), resultPage.getTotal());
    }

    default OrdersDTO selectOrder(Integer id) {
        return selectJoinOne(OrdersDTO.class,
                new MPJLambdaWrapper<OrderDO>()
                        .selectAll(OrderDO.class)
                        .select(StudentDO::getStudentName)
                        .select(StudentDO::getStudentPhoneNumber)
                        .leftJoin(StudentDO.class, StudentDO::getId, OrderDO::getStudentId)
                        .select(VipLevelDO::getLevelName)
                        .leftJoin(VipLevelDO.class, VipLevelDO::getId, OrderDO::getLevelId)
                        .selectAs(CourseDO::getName,OrdersDTO::getCommodityName)
                        .leftJoin(CourseCommodityDO.class,CourseCommodityDO::getId,OrderDO::getCommodityId)
                        .leftJoin(CourseDO.class,CourseDO::getId,CourseCommodityDO::getCourseId)
                        .eq(OrderDO::getId, id)
        );
    }

    default List<OrdersDTO> selectOrderByCommodityId(Long commodityId) {
        return selectJoinList(OrdersDTO.class,
                new MPJLambdaWrapper<OrderDO>()
                        .selectAll(OrderDO.class)
                        .select(StudentDO::getStudentName)
                        .select(StudentDO::getStudentPhoneNumber)
                        .leftJoin(StudentDO.class, StudentDO::getId, OrderDO::getStudentId)
                        .select(VipLevelDO::getLevelName)
                        .leftJoin(VipLevelDO.class, VipLevelDO::getId, OrderDO::getLevelId)
                        .selectAs(CourseDO::getName,OrdersDTO::getCommodityName)
                        .leftJoin(CourseCommodityDO.class,CourseCommodityDO::getId,OrderDO::getCommodityId)
                        .leftJoin(CourseDO.class,CourseDO::getId,CourseCommodityDO::getCourseId)
                        .eq(OrderDO::getClassesFlag, 0)
                        .eq(OrderDO::getCommodityId, commodityId)
                        .eq(OrderDO::getStatus, 1)
        );
    }

    default List<OrdersDTO> selectOrderByWxUserId(Long wxUserId, Integer status) {
        return selectJoinList(OrdersDTO.class,
                new MPJLambdaWrapper<OrderDO>()
                        .selectAll(OrderDO.class)
                        .select(StudentDO::getStudentName)
                        .select(StudentDO::getStudentPhoneNumber)
                        .leftJoin(StudentDO.class, StudentDO::getId, OrderDO::getStudentId)
                        .select(VipLevelDO::getLevelName)
                        .leftJoin(VipLevelDO.class, VipLevelDO::getId, OrderDO::getLevelId)
                        .selectAs(CourseDO::getName,OrdersDTO::getCommodityName)
                        .leftJoin(CourseCommodityDO.class,CourseCommodityDO::getId,OrderDO::getCommodityId)
                        .leftJoin(CourseDO.class,CourseDO::getId,CourseCommodityDO::getCourseId)
                        .eq(OrderDO::getWxUserId, wxUserId)
                        .eqIfExists(OrderDO::getStatus, status)
        );
    }

    default List<OrdersDTO> selectOrderByWxUserId(Long wxUserId) {
        return selectJoinList(OrdersDTO.class,
                new MPJLambdaWrapper<OrderDO>()
                        .selectAll(OrderDO.class)
                        .select(StudentDO::getStudentName)
                        .select(StudentDO::getStudentPhoneNumber)
                        .leftJoin(StudentDO.class, StudentDO::getId, OrderDO::getStudentId)
                        .select(VipLevelDO::getLevelName)
                        .leftJoin(VipLevelDO.class, VipLevelDO::getId, OrderDO::getLevelId)
                        .selectAs(CourseDO::getName,OrdersDTO::getCommodityName)
                        .leftJoin(CourseCommodityDO.class,CourseCommodityDO::getId,OrderDO::getCommodityId)
                        .leftJoin(CourseDO.class,CourseDO::getId,CourseCommodityDO::getCourseId)
                        .eq(OrderDO::getWxUserId, wxUserId)
        );
    }

    default int changeClassesFlag(Integer id, Integer classesFlag) {
        LambdaUpdateWrapper<OrderDO> wrapper = new LambdaUpdateWrapper<OrderDO>()
                .eq(OrderDO::getId, id)
                .set(OrderDO::getClassesFlag, classesFlag);
        return update(wrapper);
    }
}
